import {
  getName,
  getSex
} from './BlurArr/essentialInformationBlur'

import {
  getBankName
} from '@/api/Fillln.js'

import {
  Toast,
  Dialog
} from 'vant';
import {
  setFamilyNameFirst
} from './BlurArr/memberFamilyBlur'
import {
  useCounterStore
} from '../../pinia/user';




// 身份信息
function essentialInformationBlur(obj) {
  const {
    fromValue,
    fromScreen
  } = obj
  // 名称首字母
  getName(fromScreen, fromValue)
  // fromScreen.essentialInformation.forEach(item => {
  //   //  如果姓名为空  执行名称首字母操作  进行赋值  并且赋值为空
  //   if (item.key === 'fullName' && !item.value) getName(fromScreen, fromValue)
  //   //  如果首字母已经有了  将不再执行赋值操作
  //   if (item.key === 'fullNameFirst' && !item.value) getName(fromScreen, fromValue)
  // })

  // 根据身份证号码获取性别
  if (+fromValue.documentType === 1 && fromValue.cardId) {
    getSex(fromValue.cardId, fromScreen)
  }
}


let bankAccount = ''

async function personalDetailsBlur(obj) {
  const {
    fromValue,
    Arr,
    fromScreen,
    activeList
  } = obj
  //  政治面貌
  const politicalOutlook = personalDetailsValue(fromScreen.personalDetails, 'politicalOutlook')
  //  入党日期
  const joinCpcDate = personalDetailsValue(fromScreen.personalDetails, 'joinCpcDate')

  if (politicalOutlook.label === '中共党员') {
    joinCpcDate.rules = [{
      required: true,
      trigger: 'onChange',
      message: `请选择选择${joinCpcDate.name}`
    }]
  } else {
    if (joinCpcDate.rules.length !== 0) joinCpcDate.rules = []
  }
  // 是否验证银行卡
  if (bankAccount !== fromValue.bankAccount) {
    bankAccount = fromValue.bankAccount
    // 获取银行缩写
    const str = /^[1-9]\d{9,29}$/
    const bankName = getDetailsItem(Arr, 'bankName')
    if (str.test(fromValue.bankAccount)) {
      const res = await getBankName(fromValue.bankAccount)
      if (res.success) {
        bankName.value = res.message.value
      } else {
        bankName.value = ''
        Toast.fail(res.message);
      }
    } else {
      bankName.value = ''
    }
  }
}

function personalDetailsValue(arr, key) {
  let item
  for (let i = 0; i < arr.length; i++) {
    if (arr[i].key === key) {
      item = arr[i]
      break
    }
  }
  return item
}

function TheHouseholdRegistrationInformationBlur(obj) {
  const {
    fromValue,
    Arr,
    fromScreen,
    activeList
  } = obj

}


function contactWayBlur(obj) {
  const {
    fromValue,
    Arr,
    fromScreen,
    activeList
  } = obj

}


function addressMessageBlur(obj) {
  const {
    fromValue,
    Arr,
    fromScreen,
    activeList
  } = obj

}

function emergencyContactBlur(obj) {
  const {
    fromValue,
    Arr,
    fromScreen,
    activeList
  } = obj
  const counterStore = useCounterStore()
  if (counterStore.HireType !== 'regular' && counterStore.HireType !== 'trainee') {
    // 除了正式工其他工种动态填写紧急联系人
    dynamicStateLinkmanContacts(fromScreen)
  }

}

function dynamicStateLinkmanContacts(fromScreen) {
  let isVerify = false
  fromScreen.emergencyContact.forEach(item => {
    if (item.value && +item.value !== 0) isVerify = true
  })
  if (isVerify) {
    fromScreen.emergencyContact.forEach(item => {
      if (+item.type === 1 && item.key === 'contactMobile') {
        item.rules = [{
          required: true,
          trigger: 'onBlur',
          validator: MobilePhoneNumberVerification
        }]
      } else if (+item.type === 2) {
        item.rules = [{
          required: true,
          trigger: 'onChange',
          message: `请选择${item.name}`
        }]
      } else if (+item.type === 1) {
        item.rules = [{
          required: true,
          trigger: 'onBlur',
          message: `请输入${item.name}`
        }]
      }
    })
  } else {
    if (fromScreen.emergencyContact[1].rules.length === 1) {
      fromScreen.emergencyContact.forEach(item => {
        item.rules = []
      })
    }
  }
}

function memberFamilyBlur(obj) {
  const {
    fromValue,
    Arr,
    fromScreen,
    activeList
  } = obj
  setFamilyNameFirst(fromValue, Arr)
}

function educationalInformationBlur(obj) {
  const {
    fromValue,
    Arr,
    fromScreen,
    activeList
  } = obj

}

function occupationalHistoryBlur(obj) {
  const {
    fromValue,
    Arr,
    fromScreen,
    activeList
  } = obj

}


// 获取当前item
const getItem = function (activeList, key) {
  let it = {}
  activeList.forEach(item => {
    if (item.key === key) it = item
  })
  return it
}

// 获取单个的item
function getDetailsItem(Arr, key) {
  let item = {}
  Arr.value.forEach(it => {
    if (it.key === key) item = it
  })
  return item
}





// 手机号码
function MobilePhoneNumberVerification(value) {
  if (!value) return '请填写完整'
  const str = /^1[3-9]\d{9}$/
  if (str.test(value)) {
    return true
  } else {
    return '格式不正确'
  }
}

export default {
  essentialInformationBlur,
  personalDetailsBlur,
  TheHouseholdRegistrationInformationBlur,
  contactWayBlur,
  addressMessageBlur,
  emergencyContactBlur,
  memberFamilyBlur,
  educationalInformationBlur,
  occupationalHistoryBlur
}
